//#include <STC15F2K60S2.H>
//#include "intrins.h"
//#define DELAY_TIME 5

////总线引脚定义
//sbit SDA = P2^1;  /* 数据线 */
//sbit SCL = P2^0;  /* 时钟线 */

//void IIC_Delay(unsigned char i)
//{
//	
//    do{_nop_();}
//    while(i--);        
//}
////总线启动条件
//void IIC_Start(void)
//{
//    SDA = 1;
//    SCL = 1;
//    IIC_Delay(DELAY_TIME);
//    SDA = 0;
//    IIC_Delay(DELAY_TIME);
//    SCL = 0;	
//}

////总线停止条件
//void IIC_Stop(void)
//{
//    SDA = 0;
//    SCL = 1;
//    IIC_Delay(DELAY_TIME);
//    SDA = 1;
//    IIC_Delay(DELAY_TIME);
//}

////发送应答
//void IIC_SendAck(bit ackbit)
//{
//    SCL = 0;
//    SDA = ackbit;  					// 0：应答，1：非应答
//    IIC_Delay(DELAY_TIME);
//    SCL = 1;
//    IIC_Delay(DELAY_TIME);
//    SCL = 0; 
//    SDA = 1;
//    IIC_Delay(DELAY_TIME);
//}

////等待应答
//bit IIC_WaitAck(void)
//{
//    bit ackbit;
//    SCL  = 1;
//    IIC_Delay(DELAY_TIME);
//    ackbit = SDA;
//    SCL = 0;
//    IIC_Delay(DELAY_TIME);
//    return ackbit;
//}

////通过I2C总线发送数据
//void IIC_SendByte(unsigned char byt)
//{
//    unsigned char i;

//    for(i=0; i<8; i++)
//    {
//        SCL  = 0;
//        IIC_Delay(DELAY_TIME);
//        if(byt & 0x80) SDA  = 1;
//        else SDA  = 0;
//        IIC_Delay(DELAY_TIME);
//        SCL = 1;
//        byt <<= 1;
//        IIC_Delay(DELAY_TIME);
//    }
//    SCL  = 0;  
//}

////从I2C总线上接收数据
//unsigned char IIC_RecByte(void)
//{
//    unsigned char i, da;
//    for(i=0; i<8; i++)
//    {   
//    	SCL = 1;
//	IIC_Delay(DELAY_TIME);
//	da <<= 1;
//	if(SDA) da |= 1;
//	SCL = 0;
//	IIC_Delay(DELAY_TIME);
//    }
//    return da;    
//}
//unsigned char PCF8591_Adc(void) 
//{ 
// unsigned char temp; 
// IIC_Start(); 
// IIC_SendByte(0x90); 
// IIC_WaitAck(); 
// IIC_SendByte(0x43); // 允许 DAC，ADC 通道 3 0x43读电压值,0x41光敏电阻
// IIC_WaitAck(); 
// IIC_Start(); 
// IIC_SendByte(0x91); 
// IIC_WaitAck(); 
// temp = IIC_RecByte(); 
// IIC_SendAck(1); 
// IIC_Stop(); 
// return temp; 
//} 
//// DAC 输出
//void PCF8591_Dac(unsigned char dat) 
//{ 
// IIC_Start(); 
// IIC_SendByte(0x90); 
// IIC_WaitAck(); 
// IIC_SendByte(0x43); // 允许 DAC，ADC 通道 3 
// IIC_WaitAck(); 
// IIC_SendByte(dat); // dat-输出数模转换的数据
// IIC_WaitAck(); 
// IIC_Stop(); 
//} 
////void Seg_Proc(void){
////	if(ucSeg_Dly) return;
////	ucSeg_Dly=1;
////	ucpcf_U=PCF8591_Adc();
////	sprintf(pucSeg_Buf,"%05.2f",ucpcf_U/51.0);
////	Seg_Tran(pucSeg_Buf, pucSeg_Code);
////}
#include <STC15F2K60S2.H>
#include "intrins.h"
#define DELAY_TIME 5

//总线引脚定义
sbit sda = P2^1;  /* 数据线 */
sbit scl = P2^0;  /* 时钟线 */

static void I2C_Delay(unsigned char n)
{
    do
    {
        _nop_();	
    }
    while(n--);      	
}
//
void I2CStart(void)
{
    sda = 1;
    scl = 1;
	I2C_Delay(DELAY_TIME);
    sda = 0;
	I2C_Delay(DELAY_TIME);
    scl = 0;    
}

//
void I2CStop(void)
{
    sda = 0;
    scl = 1;
	I2C_Delay(DELAY_TIME);
    sda = 1;
	I2C_Delay(DELAY_TIME);
}

//
void I2CSendByte(unsigned char byt)
{
    unsigned char i;
	
    for(i=0; i<8; i++){
        scl = 0;
		I2C_Delay(DELAY_TIME);
        if(byt & 0x80){
            sda = 1;
        }
        else{
            sda = 0;
        }
		I2C_Delay(DELAY_TIME);
        scl = 1;
        byt <<= 1;
		I2C_Delay(DELAY_TIME);
    }
	
    scl = 0;  
}

//
unsigned char I2CReceiveByte(void)
{
	unsigned char da;
	unsigned char i;
	for(i=0;i<8;i++){   
		scl = 1;
		I2C_Delay(DELAY_TIME);
		da <<= 1;
		if(sda) 
			da |= 0x01;
		scl = 0;
		I2C_Delay(DELAY_TIME);
	}
	return da;    
}

//
unsigned char I2CWaitAck(void)
{
	unsigned char ackbit;
	
    scl = 1;
	I2C_Delay(DELAY_TIME);
    ackbit = sda; 
    scl = 0;
	I2C_Delay(DELAY_TIME);
	
	return ackbit;
}

//
void I2CSendAck(unsigned char ackbit)
{
    scl = 0;
    sda = ackbit; 
	I2C_Delay(DELAY_TIME);
    scl = 1;
	I2C_Delay(DELAY_TIME);
    scl = 0; 
	sda = 1;
	I2C_Delay(DELAY_TIME);
}

unsigned char PCF8591_Adc(void) 
{ 
 unsigned char temp; 
 I2CStart(); 
 I2CSendByte(0x90);
 I2CWaitAck();
 I2CSendByte(0x43); // 允许 DAC，ADC 通道 3 0x43读电压值,0x41光敏电阻
 I2CWaitAck();
 I2CStart(); 
 I2CSendByte(0x91); 
 I2CWaitAck();
 temp = I2CReceiveByte();
 I2CSendAck(1); 
 I2CStop(); 
 return temp; 
} 
// DAC 输出
void PCF8591_Dac(unsigned char dat) 
{ 
 I2CStart(); 
 I2CSendByte(0x90);
 I2CWaitAck();
 I2CSendByte(0x43); // 允许 DAC，ADC 通道 3 
 I2CWaitAck();
I2CSendByte(dat);  // dat-输出数模转换的数据
 I2CWaitAck();
 I2CStop(); 
} 
//void Seg_Proc(void){
//	if(ucSeg_Dly) return;
//	ucSeg_Dly=1;
//	ucpcf_U=PCF8591_Adc();
//	sprintf(pucSeg_Buf,"%05.2f",ucpcf_U/51.0);
//	Seg_Tran(pucSeg_Buf, pucSeg_Code);
//}